{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import numpy as np\n",
    "import pickle\n",
    "import multiprocessing\n",
    "from gensim.models import Word2Vec\n",
    "from gensim.models.word2vec import LineSentence\n",
    "import pandas as pd\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "data = pd.read_csv('../data/atec_nlp_sim_train.csv',sep='\\t',encoding='utf8', names=['id', 'q1', 'q2', 'label'] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>q1</th>\n",
       "      <th>q2</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>﻿怎么更改花呗手机号码</td>\n",
       "      <td>我的花呗是以前的手机号码，怎么更改成现在的支付宝的号码手机号</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>花呗的安全没有验证成功</td>\n",
       "      <td>花呗安全验证没通过怎么回事</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>借呗还款了，额度未恢复</td>\n",
       "      <td>借呗还款后额度没有恢复还显示借款</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>22</td>\n",
       "      <td>闲鱼可以使用花呗吗</td>\n",
       "      <td>花呗可以在闲鱼上面交易吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>25</td>\n",
       "      <td>花呗更改绑定银行卡</td>\n",
       "      <td>如何更换花呗绑定银行卡</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>32</td>\n",
       "      <td>花呗***期免息</td>\n",
       "      <td>蚂蚁花呗***期免息什么意思</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>34</td>\n",
       "      <td>开通花呗不用的话会有费用吗</td>\n",
       "      <td>开通花呗不用，会不会产生费用</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>42</td>\n",
       "      <td>我蚂蚁借呗放款没有到账</td>\n",
       "      <td>借呗还款为什么没有到帐</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>48</td>\n",
       "      <td>花呗怎么付款不鸟了</td>\n",
       "      <td>帮忙看一下我花呗怎么用不了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>58</td>\n",
       "      <td>支付宝花呗分期还款怎么提前还清</td>\n",
       "      <td>蚂蚁花呗分期付款可以全部提前还款吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>59</td>\n",
       "      <td>注销了一个花呗账号，新账号是否还能再开通花呗了</td>\n",
       "      <td>把以前的帐号注销，现在这个能开通花呗吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>62</td>\n",
       "      <td>为何蚂蚁借呗不能使用了</td>\n",
       "      <td>我的借呗***年能有***年怎么不能用了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>64</td>\n",
       "      <td>用了这么久的支付宝也不给开通借呗，哼</td>\n",
       "      <td>我用支付宝三四年了也不见给我开通借呗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>67</td>\n",
       "      <td>蚂蚁借呗的额度为什么会下降</td>\n",
       "      <td>为什么借呗额度被降低了，没有不良记录</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>74</td>\n",
       "      <td>我***月份花呗消费是***元，怎么要我还***元</td>\n",
       "      <td>我原本这个月总的花呗需要还***元，我还了一次***，又还了一个***，剩余的应该是***，...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>88</td>\n",
       "      <td>怎么花呗不能支付</td>\n",
       "      <td>花呗付款不了怎么回事</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>花呗支付可以使用购物津贴吗</td>\n",
       "      <td>使用购物津贴的费用可以用花呗吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>102</td>\n",
       "      <td>为什么每个人的借呗日息不一样</td>\n",
       "      <td>为什么借呗利息有的高有的底</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>109</td>\n",
       "      <td>怎么开通，花呗收款</td>\n",
       "      <td>怎么注册可以花呗收钱的</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>109</th>\n",
       "      <td>110</td>\n",
       "      <td>我的花呗怎么用这用这不让用了</td>\n",
       "      <td>我花呗为什么不能用了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>111</th>\n",
       "      <td>112</td>\n",
       "      <td>蚂蚁花呗还款期数可以更改吗</td>\n",
       "      <td>我之前设置的花呗分期还款现在可以更改吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>114</th>\n",
       "      <td>115</td>\n",
       "      <td>花呗系统繁忙 是什么意思</td>\n",
       "      <td>点进花呗为什么是系统繁忙</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>117</th>\n",
       "      <td>118</td>\n",
       "      <td>花呗交的电费为什么没有到账</td>\n",
       "      <td>我用花呗交电费，显示缴费成功，但没有到账，怎么回事</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>119</th>\n",
       "      <td>120</td>\n",
       "      <td>我借呗能不能开通</td>\n",
       "      <td>如何开启借呗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123</th>\n",
       "      <td>124</td>\n",
       "      <td>花呗自动还款需要手续费ma</td>\n",
       "      <td>花呗自动还款还要收手续费吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>133</td>\n",
       "      <td>花呗退款后为什么确认收货还是原来的价钱</td>\n",
       "      <td>退款花呗后为什么金额还是不变</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>141</td>\n",
       "      <td>为什么我账户用不了花呗</td>\n",
       "      <td>要了我这个花呗，用不了什么情况，都差不多半年了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>143</td>\n",
       "      <td>所以我今天用的花呗 就是下个月再还了</td>\n",
       "      <td>花呗是这个月花钱，下个月还款吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>145</td>\n",
       "      <td>花呗被冻结怎么恢复</td>\n",
       "      <td>花呗被封了怎么解除</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>156</th>\n",
       "      <td>157</td>\n",
       "      <td>花呗 临时额度可以分期吗</td>\n",
       "      <td>花呗的临时额度分期付款了，临时额度到期了，还可以继续分期付款吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102265</th>\n",
       "      <td>62920</td>\n",
       "      <td>余额宝怎么开通花呗自动还款</td>\n",
       "      <td>花呗到期余额宝会自动还款吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102270</th>\n",
       "      <td>62925</td>\n",
       "      <td>花呗零时怎么没了</td>\n",
       "      <td>蚂蚁花呗的临时额度怎么没有了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102274</th>\n",
       "      <td>62929</td>\n",
       "      <td>借呗这个月还款可以提前还吗</td>\n",
       "      <td>怎借呗么提前还款</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102276</th>\n",
       "      <td>62931</td>\n",
       "      <td>能否开通花呗是系统自动评估的，并不是注销后重新注册就能开通了</td>\n",
       "      <td>能否开通花呗是系统自动评估的，并不是注销后重新</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102278</th>\n",
       "      <td>62933</td>\n",
       "      <td>怎么才能快速提升借呗额度</td>\n",
       "      <td>借呗怎么可以提升额度快</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102291</th>\n",
       "      <td>62946</td>\n",
       "      <td>借呗还款金额受限制了怎么办</td>\n",
       "      <td>借呗还款额度受限制</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102302</th>\n",
       "      <td>62957</td>\n",
       "      <td>取消蚂蚁借呗授权会怎么样</td>\n",
       "      <td>蚂蚁借呗取消授权有什么影响</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102306</th>\n",
       "      <td>62961</td>\n",
       "      <td>不行你就把我花呗关了吧</td>\n",
       "      <td>算了，我把蚂蚁花呗关掉再也不用了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102312</th>\n",
       "      <td>62967</td>\n",
       "      <td>花呗不可以定酒店了吗</td>\n",
       "      <td>花呗不能使用在住酒店上了吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102317</th>\n",
       "      <td>62972</td>\n",
       "      <td>我收到短信说我使用花呗支付了什么什么，但实际上我并没有使用</td>\n",
       "      <td>我没有使用过花呗，但是却收到信息，是怎么回事</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102350</th>\n",
       "      <td>63005</td>\n",
       "      <td>借呗不到账怎么办</td>\n",
       "      <td>借呗没收到钱怎么办</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102351</th>\n",
       "      <td>63006</td>\n",
       "      <td>花呗可以分期几次</td>\n",
       "      <td>花呗最多分多少期</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102368</th>\n",
       "      <td>63023</td>\n",
       "      <td>支付宝当面付不支持花呗付款</td>\n",
       "      <td>怎么我的二维码不支持花呗付款</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102372</th>\n",
       "      <td>63027</td>\n",
       "      <td>花呗透支了没事吧</td>\n",
       "      <td>花呗可用***，还可以额外透支吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102392</th>\n",
       "      <td>63047</td>\n",
       "      <td>借呗里的钱为什么转不进银行卡</td>\n",
       "      <td>我从借呗借的钱转到银行卡里去了现在用不了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102393</th>\n",
       "      <td>63048</td>\n",
       "      <td>花呗晚还一天钱，可以吗</td>\n",
       "      <td>花呗还款，我延迟一天还可以吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102400</th>\n",
       "      <td>63055</td>\n",
       "      <td>支付宝首页没有花呗是什么原因</td>\n",
       "      <td>我的支付宝怎么没有看见 花呗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102401</th>\n",
       "      <td>63056</td>\n",
       "      <td>花呗充话费充错了手机号能不能退</td>\n",
       "      <td>我用花呗充值手机号码充错了钱可以退回来吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102402</th>\n",
       "      <td>63057</td>\n",
       "      <td>我的花呗冻结了。可不可以帮我开开</td>\n",
       "      <td>现在可以帮我开通花呗吗？我会好好用的，不再失信了</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102412</th>\n",
       "      <td>63067</td>\n",
       "      <td>我要查询是哪次花呗产生的罚款</td>\n",
       "      <td>我需要查我的花呗账单</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102417</th>\n",
       "      <td>63072</td>\n",
       "      <td>在淘宝里要怎么样才能使用花呗</td>\n",
       "      <td>淘宝用花呗怎么使用</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102443</th>\n",
       "      <td>63098</td>\n",
       "      <td>我花呗额度***了，还不能申请借呗</td>\n",
       "      <td>我蚂蚁花呗的额度都是***，为什么不能开通借呗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102444</th>\n",
       "      <td>63099</td>\n",
       "      <td>为什么身边朋友的花呗额度比我高</td>\n",
       "      <td>为什么我的花呗额度才***这么低</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102455</th>\n",
       "      <td>63110</td>\n",
       "      <td>共享单车可以使用蚂蚁花呗吗</td>\n",
       "      <td>用蚂蚁花呗能刷共享单车吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102456</th>\n",
       "      <td>63111</td>\n",
       "      <td>花呗是否可在闲鱼上使用</td>\n",
       "      <td>花呗可以用于闲鱼吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102460</th>\n",
       "      <td>63115</td>\n",
       "      <td>经常花呗分期有什么影响</td>\n",
       "      <td>花呗分期还会对芝麻分有影响吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102462</th>\n",
       "      <td>63117</td>\n",
       "      <td>为什么跨店用的购物津贴不能用花呗付款</td>\n",
       "      <td>使用花呗付款的话，还能用购物津贴和红包吗</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102465</th>\n",
       "      <td>63120</td>\n",
       "      <td>为什么不同的人借呗利息不一样</td>\n",
       "      <td>问，为什么我在借呗上借两万，分六期，为什么每期的利息都不一样</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102470</th>\n",
       "      <td>63125</td>\n",
       "      <td>支付宝客服电话多少</td>\n",
       "      <td>客服电话多少</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102473</th>\n",
       "      <td>63128</td>\n",
       "      <td>我的支付宝手机号很花呗手机号不一样怎么办</td>\n",
       "      <td>支付宝上的手机号，怎么和花呗上的不一样</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>18685 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           id                              q1  \\\n",
       "0           1                     ﻿怎么更改花呗手机号码   \n",
       "15         16                     花呗的安全没有验证成功   \n",
       "17         18                     借呗还款了，额度未恢复   \n",
       "21         22                       闲鱼可以使用花呗吗   \n",
       "24         25                       花呗更改绑定银行卡   \n",
       "31         32                        花呗***期免息   \n",
       "33         34                   开通花呗不用的话会有费用吗   \n",
       "41         42                     我蚂蚁借呗放款没有到账   \n",
       "47         48                       花呗怎么付款不鸟了   \n",
       "57         58                 支付宝花呗分期还款怎么提前还清   \n",
       "58         59         注销了一个花呗账号，新账号是否还能再开通花呗了   \n",
       "61         62                     为何蚂蚁借呗不能使用了   \n",
       "63         64              用了这么久的支付宝也不给开通借呗，哼   \n",
       "66         67                   蚂蚁借呗的额度为什么会下降   \n",
       "73         74       我***月份花呗消费是***元，怎么要我还***元   \n",
       "87         88                        怎么花呗不能支付   \n",
       "99        100                   花呗支付可以使用购物津贴吗   \n",
       "101       102                  为什么每个人的借呗日息不一样   \n",
       "108       109                       怎么开通，花呗收款   \n",
       "109       110                  我的花呗怎么用这用这不让用了   \n",
       "111       112                   蚂蚁花呗还款期数可以更改吗   \n",
       "114       115                    花呗系统繁忙 是什么意思   \n",
       "117       118                   花呗交的电费为什么没有到账   \n",
       "119       120                        我借呗能不能开通   \n",
       "123       124                   花呗自动还款需要手续费ma   \n",
       "132       133             花呗退款后为什么确认收货还是原来的价钱   \n",
       "140       141                     为什么我账户用不了花呗   \n",
       "142       143              所以我今天用的花呗 就是下个月再还了   \n",
       "144       145                       花呗被冻结怎么恢复   \n",
       "156       157                    花呗 临时额度可以分期吗   \n",
       "...       ...                             ...   \n",
       "102265  62920                   余额宝怎么开通花呗自动还款   \n",
       "102270  62925                        花呗零时怎么没了   \n",
       "102274  62929                   借呗这个月还款可以提前还吗   \n",
       "102276  62931  能否开通花呗是系统自动评估的，并不是注销后重新注册就能开通了   \n",
       "102278  62933                    怎么才能快速提升借呗额度   \n",
       "102291  62946                   借呗还款金额受限制了怎么办   \n",
       "102302  62957                    取消蚂蚁借呗授权会怎么样   \n",
       "102306  62961                     不行你就把我花呗关了吧   \n",
       "102312  62967                      花呗不可以定酒店了吗   \n",
       "102317  62972   我收到短信说我使用花呗支付了什么什么，但实际上我并没有使用   \n",
       "102350  63005                        借呗不到账怎么办   \n",
       "102351  63006                        花呗可以分期几次   \n",
       "102368  63023                   支付宝当面付不支持花呗付款   \n",
       "102372  63027                        花呗透支了没事吧   \n",
       "102392  63047                  借呗里的钱为什么转不进银行卡   \n",
       "102393  63048                     花呗晚还一天钱，可以吗   \n",
       "102400  63055                  支付宝首页没有花呗是什么原因   \n",
       "102401  63056                 花呗充话费充错了手机号能不能退   \n",
       "102402  63057                我的花呗冻结了。可不可以帮我开开   \n",
       "102412  63067                  我要查询是哪次花呗产生的罚款   \n",
       "102417  63072                  在淘宝里要怎么样才能使用花呗   \n",
       "102443  63098               我花呗额度***了，还不能申请借呗   \n",
       "102444  63099                 为什么身边朋友的花呗额度比我高   \n",
       "102455  63110                   共享单车可以使用蚂蚁花呗吗   \n",
       "102456  63111                     花呗是否可在闲鱼上使用   \n",
       "102460  63115                     经常花呗分期有什么影响   \n",
       "102462  63117              为什么跨店用的购物津贴不能用花呗付款   \n",
       "102465  63120                  为什么不同的人借呗利息不一样   \n",
       "102470  63125                       支付宝客服电话多少   \n",
       "102473  63128            我的支付宝手机号很花呗手机号不一样怎么办   \n",
       "\n",
       "                                                       q2  label  \n",
       "0                          我的花呗是以前的手机号码，怎么更改成现在的支付宝的号码手机号      1  \n",
       "15                                          花呗安全验证没通过怎么回事      1  \n",
       "17                                       借呗还款后额度没有恢复还显示借款      1  \n",
       "21                                           花呗可以在闲鱼上面交易吗      1  \n",
       "24                                            如何更换花呗绑定银行卡      1  \n",
       "31                                         蚂蚁花呗***期免息什么意思      1  \n",
       "33                                         开通花呗不用，会不会产生费用      1  \n",
       "41                                            借呗还款为什么没有到帐      1  \n",
       "47                                          帮忙看一下我花呗怎么用不了      1  \n",
       "57                                      蚂蚁花呗分期付款可以全部提前还款吗      1  \n",
       "58                                    把以前的帐号注销，现在这个能开通花呗吗      1  \n",
       "61                                   我的借呗***年能有***年怎么不能用了      1  \n",
       "63                                     我用支付宝三四年了也不见给我开通借呗      1  \n",
       "66                                     为什么借呗额度被降低了，没有不良记录      1  \n",
       "73      我原本这个月总的花呗需要还***元，我还了一次***，又还了一个***，剩余的应该是***，...      1  \n",
       "87                                             花呗付款不了怎么回事      1  \n",
       "99                                        使用购物津贴的费用可以用花呗吗      1  \n",
       "101                                         为什么借呗利息有的高有的底      1  \n",
       "108                                           怎么注册可以花呗收钱的      1  \n",
       "109                                            我花呗为什么不能用了      1  \n",
       "111                                   我之前设置的花呗分期还款现在可以更改吗      1  \n",
       "114                                          点进花呗为什么是系统繁忙      1  \n",
       "117                             我用花呗交电费，显示缴费成功，但没有到账，怎么回事      1  \n",
       "119                                                如何开启借呗      1  \n",
       "123                                         花呗自动还款还要收手续费吗      1  \n",
       "132                                        退款花呗后为什么金额还是不变      1  \n",
       "140                               要了我这个花呗，用不了什么情况，都差不多半年了      1  \n",
       "142                                       花呗是这个月花钱，下个月还款吗      1  \n",
       "144                                             花呗被封了怎么解除      1  \n",
       "156                       花呗的临时额度分期付款了，临时额度到期了，还可以继续分期付款吗      1  \n",
       "...                                                   ...    ...  \n",
       "102265                                      花呗到期余额宝会自动还款吗      1  \n",
       "102270                                     蚂蚁花呗的临时额度怎么没有了      1  \n",
       "102274                                           怎借呗么提前还款      1  \n",
       "102276                            能否开通花呗是系统自动评估的，并不是注销后重新      1  \n",
       "102278                                        借呗怎么可以提升额度快      1  \n",
       "102291                                          借呗还款额度受限制      1  \n",
       "102302                                      蚂蚁借呗取消授权有什么影响      1  \n",
       "102306                                   算了，我把蚂蚁花呗关掉再也不用了      1  \n",
       "102312                                      花呗不能使用在住酒店上了吗      1  \n",
       "102317                             我没有使用过花呗，但是却收到信息，是怎么回事      1  \n",
       "102350                                          借呗没收到钱怎么办      1  \n",
       "102351                                           花呗最多分多少期      1  \n",
       "102368                                     怎么我的二维码不支持花呗付款      1  \n",
       "102372                                   花呗可用***，还可以额外透支吗      1  \n",
       "102392                               我从借呗借的钱转到银行卡里去了现在用不了      1  \n",
       "102393                                     花呗还款，我延迟一天还可以吗      1  \n",
       "102400                                     我的支付宝怎么没有看见 花呗      1  \n",
       "102401                               我用花呗充值手机号码充错了钱可以退回来吗      1  \n",
       "102402                           现在可以帮我开通花呗吗？我会好好用的，不再失信了      1  \n",
       "102412                                         我需要查我的花呗账单      1  \n",
       "102417                                          淘宝用花呗怎么使用      1  \n",
       "102443                            我蚂蚁花呗的额度都是***，为什么不能开通借呗      1  \n",
       "102444                                   为什么我的花呗额度才***这么低      1  \n",
       "102455                                       用蚂蚁花呗能刷共享单车吗      1  \n",
       "102456                                          花呗可以用于闲鱼吗      1  \n",
       "102460                                     花呗分期还会对芝麻分有影响吗      1  \n",
       "102462                               使用花呗付款的话，还能用购物津贴和红包吗      1  \n",
       "102465                     问，为什么我在借呗上借两万，分六期，为什么每期的利息都不一样      1  \n",
       "102470                                             客服电话多少      1  \n",
       "102473                                支付宝上的手机号，怎么和花呗上的不一样      1  \n",
       "\n",
       "[18685 rows x 4 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data.label==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>q1</th>\n",
       "      <th>q2</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>也开不了花呗，就这样了？完事了</td>\n",
       "      <td>真的嘛？就是花呗付款</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>花呗冻结以后还能开通吗</td>\n",
       "      <td>我的条件可以开通花呗借款吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>如何得知关闭借呗</td>\n",
       "      <td>想永久关闭借呗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>花呗扫码付钱</td>\n",
       "      <td>二维码扫描可以用花呗吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>花呗逾期后不能分期吗</td>\n",
       "      <td>我这个 逾期后还完了 最低还款 后 能分期吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>花呗分期清空</td>\n",
       "      <td>花呗分期查询</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>借呗逾期短信通知</td>\n",
       "      <td>如何购买花呗短信通知</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>借呗即将到期要还的账单还能分期吗</td>\n",
       "      <td>借呗要分期还，是吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>花呗为什么不能支付手机交易</td>\n",
       "      <td>花呗透支了为什么不可以继续用了</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>在吗，双***有临时花呗额度吗</td>\n",
       "      <td>花呗临时额度到时间怎么办</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>我什么时候开通了花呗</td>\n",
       "      <td>查我什么时候用过，花呗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>花呗每月还了最低还款后还会有万分之五的利息吗</td>\n",
       "      <td>花呗每个月最低还款怎么算</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>我想用蚂蚁借呗怎么用不了</td>\n",
       "      <td>蚂蚁借呗设置了一次性还款，现在想分期还款，怎么操作</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>花呗也不能用了</td>\n",
       "      <td>花呗没用过</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>我开通不了借呗</td>\n",
       "      <td>我要申请借呗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>就是我花呗忘记还款了。逾期一天。有事吗</td>\n",
       "      <td>花呗账单在到期当天还款是逾期吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>我问你我借呗怎么要还钱</td>\n",
       "      <td>怎么一次性还蚂蚁借呗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>21</td>\n",
       "      <td>是否有花呗就不可以借呗</td>\n",
       "      <td>不实用花呗就没有借呗吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>23</td>\n",
       "      <td>花呗可以买手机吗</td>\n",
       "      <td>花呗逾期一天可以么</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>借呗每月还款时间</td>\n",
       "      <td>借呗多少天还款</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>26</td>\n",
       "      <td>人脸验证开通花呗</td>\n",
       "      <td>花呗怎么验证</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>借呗可以提前还第一期吗</td>\n",
       "      <td>蚂蚁借呗借款可以提前一次性还清吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>28</td>\n",
       "      <td>花呗付款成功为何美团显示支付超时</td>\n",
       "      <td>美团订单没显示付款成功但是花呗显示付了三遍订单</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>29</td>\n",
       "      <td>花呗我已经还了，现在要还借呗</td>\n",
       "      <td>我上个月已经还过的一笔，现在退回来的钱怎么还直接退回花呗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>30</td>\n",
       "      <td>国外账户可以开通借呗吗</td>\n",
       "      <td>借呗逾期还能开通吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>31</td>\n",
       "      <td>但是我银行卡支付的为什么退回花呗</td>\n",
       "      <td>我这件衣服钱款为什么退回花呗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>33</td>\n",
       "      <td>已经还了，借呗还能用吗</td>\n",
       "      <td>借呗逾期后还能借款吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>35</td>\n",
       "      <td>花呗怎样邀请好友</td>\n",
       "      <td>邀请好友开通借吧怎样邀请的</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>36</td>\n",
       "      <td>我没有这个蚂蚁借呗</td>\n",
       "      <td>我的蚂蚁借呗用不了了怎么回事？没有逾期</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>37</td>\n",
       "      <td>我昨天欠借呗多少钱</td>\n",
       "      <td>我的借呗欠了多少钱</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102438</th>\n",
       "      <td>63093</td>\n",
       "      <td>怎样退出。花呗</td>\n",
       "      <td>借呗怎么样退出</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102439</th>\n",
       "      <td>63094</td>\n",
       "      <td>没有还借呗</td>\n",
       "      <td>借呗还款暂时没能力一次还完</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102440</th>\n",
       "      <td>63095</td>\n",
       "      <td>为什么我的银行卡里的额度够还花呗的，但是付款的说还款金额大于银行限制的金额</td>\n",
       "      <td>银行卡每天限额消费一万元，怎么样还款花呗超过一万的账单</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102441</th>\n",
       "      <td>63096</td>\n",
       "      <td>借呗逾期还请后还能使用吗</td>\n",
       "      <td>能不能申请借呗分期</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102442</th>\n",
       "      <td>63097</td>\n",
       "      <td>蚂蚁借呗逾期***天以后还款还能继续贷款吗</td>\n",
       "      <td>蚂蚁借呗，可以只借***天吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102445</th>\n",
       "      <td>63100</td>\n",
       "      <td>怎么老开通不了借呗</td>\n",
       "      <td>借呗的一万额度怎么没了</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102446</th>\n",
       "      <td>63101</td>\n",
       "      <td>我的花呗很多东西都不支持付款，怎么回事</td>\n",
       "      <td>为什么商家支持用花呗，我也首选花呗付款，反而不支持花呗支付</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102447</th>\n",
       "      <td>63102</td>\n",
       "      <td>我想提前把花呗还了</td>\n",
       "      <td>花呗逾期提前还</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102448</th>\n",
       "      <td>63103</td>\n",
       "      <td>商户最多可以有多少花呗收款额度</td>\n",
       "      <td>花呗退款后额度恢复多少</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102449</th>\n",
       "      <td>63104</td>\n",
       "      <td>借呗的按期还款可以设置为手动还款吗</td>\n",
       "      <td>借呗自动还款失败后手动还款</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102450</th>\n",
       "      <td>63105</td>\n",
       "      <td>转账不可以用花呗里面的钱吗</td>\n",
       "      <td>花呗里的钱可以转到支付宝余额里吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102451</th>\n",
       "      <td>63106</td>\n",
       "      <td>我为啥没有 花呗和借呗</td>\n",
       "      <td>借呗和花呗为什么用不了</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102452</th>\n",
       "      <td>63107</td>\n",
       "      <td>花呗是使用和借呗的使用冲突吗</td>\n",
       "      <td>花呗和借呗一样的是吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102453</th>\n",
       "      <td>63108</td>\n",
       "      <td>为什么本月花呗账单不能分期</td>\n",
       "      <td>花呗分期账单数额错误</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102454</th>\n",
       "      <td>63109</td>\n",
       "      <td>如何关闭首选花呗</td>\n",
       "      <td>花呗余额关闭</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102457</th>\n",
       "      <td>63112</td>\n",
       "      <td>我是***月***号用的花呗，是不是只要在***月***号还完就不用利息</td>\n",
       "      <td>不使用花呗是不是就不用还利息</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102458</th>\n",
       "      <td>63113</td>\n",
       "      <td>借呗可逾期多久</td>\n",
       "      <td>逾期了两次还可以借呗不</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102459</th>\n",
       "      <td>63114</td>\n",
       "      <td>我已经通过淘宝给商家付了七十九，为什么花呗上面显示我还应该还***</td>\n",
       "      <td>我已经用花呗支付过***次了，为什么没显示</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102461</th>\n",
       "      <td>63116</td>\n",
       "      <td>直接花呗界面这个账单多好</td>\n",
       "      <td>花呗已经关了，现在这个账户，然后开通不了花呗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102463</th>\n",
       "      <td>63118</td>\n",
       "      <td>我的花呗什么时候能用</td>\n",
       "      <td>花呗到什么时候收钱</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102464</th>\n",
       "      <td>63119</td>\n",
       "      <td>每个商品单独付款都是可以花呗</td>\n",
       "      <td>是不是所有可以支付宝付款的店家都可以用花呗付款</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102466</th>\n",
       "      <td>63121</td>\n",
       "      <td>花呗的花呗的款式，确认收货以后才扣的吗</td>\n",
       "      <td>使用花呗付款要点确认收货吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102467</th>\n",
       "      <td>63122</td>\n",
       "      <td>花呗分期，影响借呗吗</td>\n",
       "      <td>我的借呗每个月分期准时还会影响信用吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102468</th>\n",
       "      <td>63123</td>\n",
       "      <td>借呗怎么还款</td>\n",
       "      <td>我想从借唄借两万，按期还款，每月应该还多少，利息怎么算</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102469</th>\n",
       "      <td>63124</td>\n",
       "      <td>如何设置花呗改款顺序</td>\n",
       "      <td>蚂蚁花呗还款顺序</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102471</th>\n",
       "      <td>63126</td>\n",
       "      <td>我在淘宝买东西的话，钱不够时可以使用花呗吗</td>\n",
       "      <td>我买东西不是在淘宝上买能用花呗吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102472</th>\n",
       "      <td>63127</td>\n",
       "      <td>花呗分期还一期后能用吗</td>\n",
       "      <td>分期是还花呗吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102474</th>\n",
       "      <td>63129</td>\n",
       "      <td>借呗这个月的分期晚几天还可以吗</td>\n",
       "      <td>借呗分期后可以更改分期时间吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102475</th>\n",
       "      <td>63130</td>\n",
       "      <td>我怎么没有花呗临时额度了</td>\n",
       "      <td>花呗有零时额度吗</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102476</th>\n",
       "      <td>63131</td>\n",
       "      <td>怎么授权芝麻信用给花呗</td>\n",
       "      <td>花呗授权联系人怎么授权</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>83792 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           id                                     q1  \\\n",
       "1           2                        也开不了花呗，就这样了？完事了   \n",
       "2           3                            花呗冻结以后还能开通吗   \n",
       "3           4                               如何得知关闭借呗   \n",
       "4           5                                 花呗扫码付钱   \n",
       "5           6                             花呗逾期后不能分期吗   \n",
       "6           7                                 花呗分期清空   \n",
       "7           8                               借呗逾期短信通知   \n",
       "8           9                       借呗即将到期要还的账单还能分期吗   \n",
       "9          10                          花呗为什么不能支付手机交易   \n",
       "10         11                        在吗，双***有临时花呗额度吗   \n",
       "11         12                             我什么时候开通了花呗   \n",
       "12         13                 花呗每月还了最低还款后还会有万分之五的利息吗   \n",
       "13         14                           我想用蚂蚁借呗怎么用不了   \n",
       "14         15                                花呗也不能用了   \n",
       "16         17                                我开通不了借呗   \n",
       "18         19                    就是我花呗忘记还款了。逾期一天。有事吗   \n",
       "19         20                            我问你我借呗怎么要还钱   \n",
       "20         21                            是否有花呗就不可以借呗   \n",
       "22         23                               花呗可以买手机吗   \n",
       "23         24                               借呗每月还款时间   \n",
       "25         26                               人脸验证开通花呗   \n",
       "26         27                            借呗可以提前还第一期吗   \n",
       "27         28                       花呗付款成功为何美团显示支付超时   \n",
       "28         29                         花呗我已经还了，现在要还借呗   \n",
       "29         30                            国外账户可以开通借呗吗   \n",
       "30         31                       但是我银行卡支付的为什么退回花呗   \n",
       "32         33                            已经还了，借呗还能用吗   \n",
       "34         35                               花呗怎样邀请好友   \n",
       "35         36                              我没有这个蚂蚁借呗   \n",
       "36         37                              我昨天欠借呗多少钱   \n",
       "...       ...                                    ...   \n",
       "102438  63093                                怎样退出。花呗   \n",
       "102439  63094                                  没有还借呗   \n",
       "102440  63095  为什么我的银行卡里的额度够还花呗的，但是付款的说还款金额大于银行限制的金额   \n",
       "102441  63096                           借呗逾期还请后还能使用吗   \n",
       "102442  63097                  蚂蚁借呗逾期***天以后还款还能继续贷款吗   \n",
       "102445  63100                              怎么老开通不了借呗   \n",
       "102446  63101                    我的花呗很多东西都不支持付款，怎么回事   \n",
       "102447  63102                              我想提前把花呗还了   \n",
       "102448  63103                        商户最多可以有多少花呗收款额度   \n",
       "102449  63104                      借呗的按期还款可以设置为手动还款吗   \n",
       "102450  63105                          转账不可以用花呗里面的钱吗   \n",
       "102451  63106                            我为啥没有 花呗和借呗   \n",
       "102452  63107                         花呗是使用和借呗的使用冲突吗   \n",
       "102453  63108                          为什么本月花呗账单不能分期   \n",
       "102454  63109                               如何关闭首选花呗   \n",
       "102457  63112   我是***月***号用的花呗，是不是只要在***月***号还完就不用利息   \n",
       "102458  63113                                借呗可逾期多久   \n",
       "102459  63114      我已经通过淘宝给商家付了七十九，为什么花呗上面显示我还应该还***   \n",
       "102461  63116                           直接花呗界面这个账单多好   \n",
       "102463  63118                             我的花呗什么时候能用   \n",
       "102464  63119                         每个商品单独付款都是可以花呗   \n",
       "102466  63121                    花呗的花呗的款式，确认收货以后才扣的吗   \n",
       "102467  63122                             花呗分期，影响借呗吗   \n",
       "102468  63123                                 借呗怎么还款   \n",
       "102469  63124                             如何设置花呗改款顺序   \n",
       "102471  63126                  我在淘宝买东西的话，钱不够时可以使用花呗吗   \n",
       "102472  63127                            花呗分期还一期后能用吗   \n",
       "102474  63129                        借呗这个月的分期晚几天还可以吗   \n",
       "102475  63130                           我怎么没有花呗临时额度了   \n",
       "102476  63131                            怎么授权芝麻信用给花呗   \n",
       "\n",
       "                                   q2  label  \n",
       "1                          真的嘛？就是花呗付款      0  \n",
       "2                       我的条件可以开通花呗借款吗      0  \n",
       "3                             想永久关闭借呗      0  \n",
       "4                         二维码扫描可以用花呗吗      0  \n",
       "5              我这个 逾期后还完了 最低还款 后 能分期吗      0  \n",
       "6                              花呗分期查询      0  \n",
       "7                          如何购买花呗短信通知      0  \n",
       "8                           借呗要分期还，是吗      0  \n",
       "9                     花呗透支了为什么不可以继续用了      0  \n",
       "10                       花呗临时额度到时间怎么办      0  \n",
       "11                        查我什么时候用过，花呗      0  \n",
       "12                       花呗每个月最低还款怎么算      0  \n",
       "13          蚂蚁借呗设置了一次性还款，现在想分期还款，怎么操作      0  \n",
       "14                              花呗没用过      0  \n",
       "16                             我要申请借呗      0  \n",
       "18                    花呗账单在到期当天还款是逾期吗      0  \n",
       "19                         怎么一次性还蚂蚁借呗      0  \n",
       "20                        不实用花呗就没有借呗吗      0  \n",
       "22                          花呗逾期一天可以么      0  \n",
       "23                            借呗多少天还款      0  \n",
       "25                             花呗怎么验证      0  \n",
       "26                   蚂蚁借呗借款可以提前一次性还清吗      0  \n",
       "27            美团订单没显示付款成功但是花呗显示付了三遍订单      0  \n",
       "28       我上个月已经还过的一笔，现在退回来的钱怎么还直接退回花呗      0  \n",
       "29                          借呗逾期还能开通吗      0  \n",
       "30                     我这件衣服钱款为什么退回花呗      0  \n",
       "32                         借呗逾期后还能借款吗      0  \n",
       "34                      邀请好友开通借吧怎样邀请的      0  \n",
       "35                我的蚂蚁借呗用不了了怎么回事？没有逾期      0  \n",
       "36                          我的借呗欠了多少钱      0  \n",
       "...                               ...    ...  \n",
       "102438                        借呗怎么样退出      0  \n",
       "102439                  借呗还款暂时没能力一次还完      0  \n",
       "102440    银行卡每天限额消费一万元，怎么样还款花呗超过一万的账单      0  \n",
       "102441                      能不能申请借呗分期      0  \n",
       "102442                 蚂蚁借呗，可以只借***天吗      0  \n",
       "102445                    借呗的一万额度怎么没了      0  \n",
       "102446  为什么商家支持用花呗，我也首选花呗付款，反而不支持花呗支付      0  \n",
       "102447                        花呗逾期提前还      0  \n",
       "102448                    花呗退款后额度恢复多少      0  \n",
       "102449                  借呗自动还款失败后手动还款      0  \n",
       "102450               花呗里的钱可以转到支付宝余额里吗      0  \n",
       "102451                    借呗和花呗为什么用不了      0  \n",
       "102452                     花呗和借呗一样的是吗      0  \n",
       "102453                     花呗分期账单数额错误      0  \n",
       "102454                         花呗余额关闭      0  \n",
       "102457                 不使用花呗是不是就不用还利息      0  \n",
       "102458                    逾期了两次还可以借呗不      0  \n",
       "102459          我已经用花呗支付过***次了，为什么没显示      0  \n",
       "102461         花呗已经关了，现在这个账户，然后开通不了花呗      0  \n",
       "102463                      花呗到什么时候收钱      0  \n",
       "102464        是不是所有可以支付宝付款的店家都可以用花呗付款      0  \n",
       "102466                  使用花呗付款要点确认收货吗      0  \n",
       "102467             我的借呗每个月分期准时还会影响信用吗      0  \n",
       "102468    我想从借唄借两万，按期还款，每月应该还多少，利息怎么算      0  \n",
       "102469                       蚂蚁花呗还款顺序      0  \n",
       "102471               我买东西不是在淘宝上买能用花呗吗      0  \n",
       "102472                        分期是还花呗吗      0  \n",
       "102474                 借呗分期后可以更改分期时间吗      0  \n",
       "102475                       花呗有零时额度吗      0  \n",
       "102476                    花呗授权联系人怎么授权      0  \n",
       "\n",
       "[83792 rows x 4 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data.label==0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    83792\n",
       "1    18685\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.label.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache /tmp/jieba.cache\n",
      "Loading model cost 7.165 seconds.\n",
      "Prefix dict has been built succesfully.\n"
     ]
    }
   ],
   "source": [
    "data['q1_cut']=data['q1'].map(lambda x:list(jieba.cut(x)))\n",
    "data['q2_cut']=data['q2'].map(lambda x:list(jieba.cut(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data.to_csv('../data/cut.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "content =list(data.q1_cut)+list(data.q2_cut)\n",
    "\n",
    "\n",
    "model = Word2Vec(content, size=config['w2v_vec_dim'], window=5, min_count=5, workers=multiprocessing.cpu_count()\n",
    "                 )\n",
    "model.save(config['w2v_content_word_model'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2552, 256)\n",
      "save\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: Call to deprecated `syn0` (Attribute will be removed in 4.0.0, use self.wv.vectors instead).\n",
      "  if __name__ == '__main__':\n"
     ]
    }
   ],
   "source": [
    "weights = model.wv.syn0\n",
    "vocab = dict([(k, v.index + 1) for k, v in model.wv.vocab.items()])\n",
    "vocab['<-UNKNOW->'] = len(vocab) + 1\n",
    "embed_weights = np.zeros(shape=(weights.shape[0] + 2, weights.shape[1]))\n",
    "embed_weights[1:weights.shape[0] + 1] = weights\n",
    "unk_vec = np.random.random(size=weights.shape[1]) * 0.5\n",
    "pading_vec = np.random.random(size=weights.shape[1]) * 0\n",
    "embed_weights[weights.shape[0] + 1] = unk_vec - unk_vec.mean()\n",
    "embed_weights[0] = pading_vec\n",
    "\n",
    "#pickle.dump(vocab, open(config['word_embed_dict'], \"wb\"))\n",
    "\n",
    "config['vocab_size'] = embed_weights.shape[0]\n",
    "config['w2v_vec_dim'] = embed_weights.shape[1]\n",
    "yaml.dump(config, open('config.yaml', 'w'))\n",
    "\n",
    "np.save(config['word_embed_weight'], embed_weights)\n",
    "print(embed_weights.shape)\n",
    "print('save')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "word_voc = vocab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "def padding_id(ids, padding_token=0, padding_length=None):\n",
    "    if len(ids) > padding_length:\n",
    "        return ids[:padding_length]\n",
    "    else:\n",
    "        return ids + [padding_token] * (padding_length - len(ids))\n",
    "\n",
    "\n",
    "def word2id(contents):\n",
    "    ''' contents  list\n",
    "    '''\n",
    "#     contents = str(contents)\n",
    "#     contents = contents.split()\n",
    "\n",
    "    ids = [word_voc[c] if c in word_voc else len(word_voc) for c in contents]\n",
    "\n",
    "    return padding_id(ids, padding_token=0, padding_length=config['word_maxlen'])\n",
    "\n",
    "\n",
    "def data2id():\n",
    "\n",
    "    data = read_df(config['train_cut'])\n",
    "    data['title'] = data.title.map(lambda x: word2id(x))\n",
    "    data['content'] = data.content.map(lambda x: word2id(x))\n",
    "    print(data.head(4))\n",
    "    data.to_pickle(config['train_cut_id_dump'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data['q1_cut_id'] = data['q1_cut'].map(lambda x: word2id(x))\n",
    "data['q2_cut_id'] = data['q2_cut'].map(lambda x: word2id(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>q1</th>\n",
       "      <th>q2</th>\n",
       "      <th>label</th>\n",
       "      <th>q1_cut</th>\n",
       "      <th>q2_cut</th>\n",
       "      <th>q1_cut_id</th>\n",
       "      <th>q2_cut_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>﻿1</td>\n",
       "      <td>﻿怎么更改花呗手机号码</td>\n",
       "      <td>我的花呗是以前的手机号码，怎么更改成现在的支付宝的号码手机号</td>\n",
       "      <td>1</td>\n",
       "      <td>[﻿, 怎么, 更改, 花, 呗, 手机号码]</td>\n",
       "      <td>[我, 的, 花, 呗, 是, 以前, 的, 手机号码, ，, 怎么, 更, 改成, 现在,...</td>\n",
       "      <td>[2551, 10, 233, 2, 1, 216, 0, 0, 0, 0, 0, 0, 0...</td>\n",
       "      <td>[5, 4, 2, 1, 19, 159, 4, 216, 8, 10, 986, 603,...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>也开不了花呗，就这样了？完事了</td>\n",
       "      <td>真的嘛？就是花呗付款</td>\n",
       "      <td>0</td>\n",
       "      <td>[也, 开, 不了, 花, 呗, ，, 就, 这样, 了, ？, 完事, 了]</td>\n",
       "      <td>[真的, 嘛, ？, 就是, 花, 呗, 付款]</td>\n",
       "      <td>[103, 160, 32, 2, 1, 8, 74, 623, 7, 139, 2479,...</td>\n",
       "      <td>[802, 131, 139, 225, 2, 1, 35, 0, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>花呗冻结以后还能开通吗</td>\n",
       "      <td>我的条件可以开通花呗借款吗</td>\n",
       "      <td>0</td>\n",
       "      <td>[花, 呗, 冻结, 以后, 还, 能, 开通, 吗]</td>\n",
       "      <td>[我, 的, 条件, 可以, 开通, 花, 呗, 借款, 吗]</td>\n",
       "      <td>[2, 1, 110, 178, 11, 23, 20, 9, 0, 0, 0, 0, 0,...</td>\n",
       "      <td>[5, 4, 186, 14, 20, 2, 1, 120, 9, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>如何得知关闭借呗</td>\n",
       "      <td>想永久关闭借呗</td>\n",
       "      <td>0</td>\n",
       "      <td>[如何, 得知, 关闭, 借, 呗]</td>\n",
       "      <td>[想, 永久, 关闭, 借, 呗]</td>\n",
       "      <td>[55, 2551, 53, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0...</td>\n",
       "      <td>[70, 574, 53, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>花呗扫码付钱</td>\n",
       "      <td>二维码扫描可以用花呗吗</td>\n",
       "      <td>0</td>\n",
       "      <td>[花, 呗, 扫码, 付钱]</td>\n",
       "      <td>[二维码, 扫描, 可以, 用花, 呗, 吗]</td>\n",
       "      <td>[2, 1, 311, 556, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...</td>\n",
       "      <td>[210, 868, 14, 33, 1, 9, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id               q1                              q2  label  \\\n",
       "0  ﻿1      ﻿怎么更改花呗手机号码  我的花呗是以前的手机号码，怎么更改成现在的支付宝的号码手机号      1   \n",
       "1   2  也开不了花呗，就这样了？完事了                      真的嘛？就是花呗付款      0   \n",
       "2   3      花呗冻结以后还能开通吗                   我的条件可以开通花呗借款吗      0   \n",
       "3   4         如何得知关闭借呗                         想永久关闭借呗      0   \n",
       "4   5           花呗扫码付钱                     二维码扫描可以用花呗吗      0   \n",
       "\n",
       "                                    q1_cut  \\\n",
       "0                  [﻿, 怎么, 更改, 花, 呗, 手机号码]   \n",
       "1  [也, 开, 不了, 花, 呗, ，, 就, 这样, 了, ？, 完事, 了]   \n",
       "2              [花, 呗, 冻结, 以后, 还, 能, 开通, 吗]   \n",
       "3                       [如何, 得知, 关闭, 借, 呗]   \n",
       "4                           [花, 呗, 扫码, 付钱]   \n",
       "\n",
       "                                              q2_cut  \\\n",
       "0  [我, 的, 花, 呗, 是, 以前, 的, 手机号码, ，, 怎么, 更, 改成, 现在,...   \n",
       "1                           [真的, 嘛, ？, 就是, 花, 呗, 付款]   \n",
       "2                    [我, 的, 条件, 可以, 开通, 花, 呗, 借款, 吗]   \n",
       "3                                  [想, 永久, 关闭, 借, 呗]   \n",
       "4                            [二维码, 扫描, 可以, 用花, 呗, 吗]   \n",
       "\n",
       "                                           q1_cut_id  \\\n",
       "0  [2551, 10, 233, 2, 1, 216, 0, 0, 0, 0, 0, 0, 0...   \n",
       "1  [103, 160, 32, 2, 1, 8, 74, 623, 7, 139, 2479,...   \n",
       "2  [2, 1, 110, 178, 11, 23, 20, 9, 0, 0, 0, 0, 0,...   \n",
       "3  [55, 2551, 53, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0...   \n",
       "4  [2, 1, 311, 556, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...   \n",
       "\n",
       "                                           q2_cut_id  \n",
       "0  [5, 4, 2, 1, 19, 159, 4, 216, 8, 10, 986, 603,...  \n",
       "1  [802, 131, 139, 225, 2, 1, 35, 0, 0, 0, 0, 0, ...  \n",
       "2  [5, 4, 186, 14, 20, 2, 1, 120, 9, 0, 0, 0, 0, ...  \n",
       "3  [70, 574, 53, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,...  \n",
       "4  [210, 868, 14, 33, 1, 9, 0, 0, 0, 0, 0, 0, 0, ...  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import keras\n",
    "from keras.models import *\n",
    "from keras.layers import *\n",
    "from keras.optimizers import *\n",
    "from keras.preprocessing import sequence\n",
    "from keras.regularizers import l2\n",
    "from keras import backend as K\n",
    "from keras.engine.topology import Layer\n",
    "from keras.backend.tensorflow_backend import set_session\n",
    "import time\n",
    "from tensorflow.contrib import learn\n",
    "import yaml\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import precision_score, recall_score, f1_score\n",
    "import numpy as np\n",
    "from keras import backend as K\n",
    "\n",
    "params = yaml.load(open('params.yaml', 'r'))\n",
    "config = yaml.load(open('config.yaml', 'r'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def convs_block(data, convs=[3,4,5], f=256, name=\"conv_feat\"):\n",
    "    pools = []\n",
    "    for c in convs:\n",
    "        conv = Activation(activation=\"relu\")(BatchNormalization()(\n",
    "            Conv1D(filters=f, kernel_size=c, padding=\"valid\")(data)))\n",
    "        pool = GlobalMaxPool1D()(conv)\n",
    "        pools.append(pool)\n",
    "    return concatenate(pools, name=name)\n",
    "\n",
    "\n",
    "def convs_block2(data, convs=[3, 4, 5], f=256, name=\"conv_feat\"):\n",
    "    pools = []\n",
    "    for c in convs:\n",
    "        conv = Activation(activation=\"relu\")(BatchNormalization()(\n",
    "            Conv1D(filters=f, kernel_size=c, padding=\"valid\")(data)))\n",
    "        conv = MaxPool1D(pool_size=10)(conv)\n",
    "        conv = Activation(activation=\"relu\")(BatchNormalization()(\n",
    "            Conv1D(filters=f, kernel_size=c, padding=\"valid\")(conv)))\n",
    "\n",
    "        pool = GlobalMaxPool1D()(conv)\n",
    "        pools.append(pool)\n",
    "    return concatenate(pools, name=name)\n",
    "\n",
    "\n",
    "def get_textcnn(seq_length, embed_weight, pretrain=False):\n",
    "    content = Input(shape=(seq_length,), dtype=\"int32\")\n",
    "    if pretrain:\n",
    "        embedding = Embedding(name='word_embedding', input_dim=config[\n",
    "                              'vocab_size'], weights=[embed_weight], output_dim=config['w2v_vec_dim'], trainable=False)\n",
    "    else:\n",
    "        embedding = Embedding(name='word_embedding', input_dim=config[\n",
    "                              'vocab_size'], output_dim=config['w2v_vec_dim'], trainable=True)\n",
    "    trans_content = Activation(activation=\"relu\")(\n",
    "        BatchNormalization()((TimeDistributed(Dense(256))(embedding(content)))))\n",
    "    feat = convs_block(trans_content)\n",
    "    dropfeat = Dropout(0.2)(feat)\n",
    "    fc = Activation(activation=\"relu\")(\n",
    "        BatchNormalization()(Dense(256)(dropfeat)))\n",
    "    output = Dense(2, activation=\"softmax\")(fc)\n",
    "    model = Model(inputs=content, outputs=output)\n",
    "    model.compile(loss='categorical_crossentropy',\n",
    "                  optimizer=\"adam\", metrics=['accuracy'])\n",
    "    model.summary()\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pickle\n",
    "import keras\n",
    "from keras.models import *\n",
    "from keras.layers import *\n",
    "from keras.optimizers import *\n",
    "from keras.preprocessing import sequence\n",
    "from keras.regularizers import l2\n",
    "from keras import backend as K\n",
    "from keras.engine.topology import Layer\n",
    "from keras.backend.tensorflow_backend import set_session\n",
    "#from recurrentshop import *\n",
    "import time\n",
    "from tensorflow.contrib import learn\n",
    "import yaml\n",
    "from sklearn.model_selection import train_test_split\n",
    "import keras.backend as K\n",
    "from keras.callbacks import TensorBoard\n",
    "params = yaml.load(open('params.yaml', 'r'))\n",
    "config = yaml.load(open('config.yaml', 'r'))\n",
    "\n",
    "from keras.callbacks import Callback\n",
    "from sklearn.metrics import confusion_matrix, accuracy_score, f1_score, precision_score, recall_score\n",
    "\n",
    "\n",
    "\n",
    "def score(label, pred, gate=0.5):\n",
    "\n",
    "    if len(label.shape) == 1:\n",
    "        p = (pred > gate).astype(\"int\")\n",
    "        p = np.squeeze(p)\n",
    "        l = label\n",
    "    else:\n",
    "        p = np.argmax(pred, axis=1)\n",
    "        l = np.argmax(label, axis=1)\n",
    "\n",
    "    #print(confusion_matrix(l, p).view())\n",
    "    pre_score = precision_score(l, p, pos_label=1, average='binary')\n",
    "    rec_score = recall_score(l, p, pos_label=1, average='binary')\n",
    "    f_score = f1_score(l, p)\n",
    "    return pre_score, rec_score, f_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "q1_id = np.array(list(data.q1_cut_id.values))\n",
    "q2_id = np.array(list(data.q2_cut_id.values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(39346, 20)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q1_id.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(39346, 40)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([q1_id,q2_id],1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data['label'] = data['label'].apply(\n",
    "        lambda x: int(x ==1))  # 人写的是1 机器写的是0\n",
    "X = np.concatenate([q1_id,q2_id],1)\n",
    "Y = keras.utils.to_categorical(list(data.label), num_classes=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x_train, x_dev, y_train, y_dev = train_test_split(X, Y,    test_size=0.1,     random_state=2008)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def train_batch_generator(x_source, y_source, batch):\n",
    "    while True:\n",
    "        batch_list_x = []\n",
    "        batch_list_y = []\n",
    "        for line, y in zip(x_source, y_source):\n",
    "            x = line.astype('float32')\n",
    "            batch_list_x.append(x)\n",
    "            batch_list_y.append(y)\n",
    "            if len(batch_list_x) == batch:\n",
    "                yield (np.array(batch_list_x), np.array(batch_list_y))\n",
    "                batch_list_x = []\n",
    "                batch_list_y = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__________________________________________________________________________________________________\n",
      "Layer (type)                    Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      "input_4 (InputLayer)            (None, 40)           0                                            \n",
      "__________________________________________________________________________________________________\n",
      "word_embedding (Embedding)      (None, 40, 256)      653312      input_4[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "time_distributed_4 (TimeDistrib (None, 40, 256)      65792       word_embedding[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_20 (BatchNo (None, 40, 256)      1024        time_distributed_4[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "activation_20 (Activation)      (None, 40, 256)      0           batch_normalization_20[0][0]     \n",
      "__________________________________________________________________________________________________\n",
      "conv1d_14 (Conv1D)              (None, 38, 256)      196864      activation_20[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "conv1d_15 (Conv1D)              (None, 37, 256)      262400      activation_20[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "conv1d_16 (Conv1D)              (None, 36, 256)      327936      activation_20[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_21 (BatchNo (None, 38, 256)      1024        conv1d_14[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_22 (BatchNo (None, 37, 256)      1024        conv1d_15[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_23 (BatchNo (None, 36, 256)      1024        conv1d_16[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "activation_21 (Activation)      (None, 38, 256)      0           batch_normalization_21[0][0]     \n",
      "__________________________________________________________________________________________________\n",
      "activation_22 (Activation)      (None, 37, 256)      0           batch_normalization_22[0][0]     \n",
      "__________________________________________________________________________________________________\n",
      "activation_23 (Activation)      (None, 36, 256)      0           batch_normalization_23[0][0]     \n",
      "__________________________________________________________________________________________________\n",
      "global_max_pooling1d_14 (Global (None, 256)          0           activation_21[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "global_max_pooling1d_15 (Global (None, 256)          0           activation_22[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "global_max_pooling1d_16 (Global (None, 256)          0           activation_23[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "conv_feat (Concatenate)         (None, 768)          0           global_max_pooling1d_14[0][0]    \n",
      "                                                                 global_max_pooling1d_15[0][0]    \n",
      "                                                                 global_max_pooling1d_16[0][0]    \n",
      "__________________________________________________________________________________________________\n",
      "dropout_4 (Dropout)             (None, 768)          0           conv_feat[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "dense_11 (Dense)                (None, 256)          196864      dropout_4[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_24 (BatchNo (None, 256)          1024        dense_11[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "activation_24 (Activation)      (None, 256)          0           batch_normalization_24[0][0]     \n",
      "__________________________________________________________________________________________________\n",
      "dense_12 (Dense)                (None, 2)            514         activation_24[0][0]              \n",
      "==================================================================================================\n",
      "Total params: 1,708,802\n",
      "Trainable params: 1,706,242\n",
      "Non-trainable params: 2,560\n",
      "__________________________________________________________________________________________________\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 233s 843ms/step - loss: 0.5424 - acc: 0.7645 - val_loss: 0.6513 - val_acc: 0.7855\n",
      "('p r f1 ', 1.0, 0.007058823529411765, 0.014018691588785047)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 229s 831ms/step - loss: 0.4759 - acc: 0.7880 - val_loss: 0.5375 - val_acc: 0.7896\n",
      "('p r f1 ', 0.59482758620689657, 0.081176470588235294, 0.14285714285714285)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 229s 828ms/step - loss: 0.4397 - acc: 0.8035 - val_loss: 0.5999 - val_acc: 0.7098\n",
      "('p r f1 ', 0.34823284823284822, 0.39411764705882352, 0.36975717439293598)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 229s 828ms/step - loss: 0.4016 - acc: 0.8194 - val_loss: 0.5772 - val_acc: 0.7166\n",
      "('p r f1 ', 0.35326688815060908, 0.37529411764705883, 0.36394751853964635)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 227s 823ms/step - loss: 0.3589 - acc: 0.8407 - val_loss: 0.6428 - val_acc: 0.7271\n",
      "('p r f1 ', 0.36407766990291263, 0.35294117647058826, 0.35842293906810041)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 227s 823ms/step - loss: 0.3178 - acc: 0.8600 - val_loss: 0.6930 - val_acc: 0.7685\n",
      "('p r f1 ', 0.41952506596306066, 0.18705882352941178, 0.25874694873881204)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 228s 826ms/step - loss: 0.2777 - acc: 0.8802 - val_loss: 0.7691 - val_acc: 0.7588\n",
      "('p r f1 ', 0.39445628997867804, 0.21764705882352942, 0.28051554207733137)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 231s 836ms/step - loss: 0.2460 - acc: 0.8957 - val_loss: 0.7811 - val_acc: 0.7543\n",
      "('p r f1 ', 0.39572192513368987, 0.26117647058823529, 0.31467044649184972)\n",
      "Epoch 1/1\n",
      " 32/276 [==>...........................] - ETA: 3:14 - loss: 0.2511 - acc: 0.8945"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-35-0916f334efbe>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     10\u001b[0m         \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m         \u001b[0msteps_per_epoch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'batch_size'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m         \u001b[0mvalidation_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_dev\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_dev\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m     )\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/keras/legacy/interfaces.pyc\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     89\u001b[0m                 warnings.warn('Update your `' + object_name +\n\u001b[1;32m     90\u001b[0m                               '` call to the Keras 2 API: ' + signature, stacklevel=2)\n\u001b[0;32m---> 91\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     92\u001b[0m         \u001b[0mwrapper\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_original_function\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     93\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/keras/engine/training.pyc\u001b[0m in \u001b[0;36mfit_generator\u001b[0;34m(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)\u001b[0m\n\u001b[1;32m   2222\u001b[0m                     outs = self.train_on_batch(x, y,\n\u001b[1;32m   2223\u001b[0m                                                \u001b[0msample_weight\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msample_weight\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2224\u001b[0;31m                                                class_weight=class_weight)\n\u001b[0m\u001b[1;32m   2225\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2226\u001b[0m                     \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mouts\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/keras/engine/training.pyc\u001b[0m in \u001b[0;36mtrain_on_batch\u001b[0;34m(self, x, y, sample_weight, class_weight)\u001b[0m\n\u001b[1;32m   1881\u001b[0m             \u001b[0mins\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0msample_weights\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1882\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_train_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1883\u001b[0;31m         \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1884\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1885\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m   2476\u001b[0m         \u001b[0msession\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_session\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2477\u001b[0m         updated = session.run(fetches=fetches, feed_dict=feed_dict,\n\u001b[0;32m-> 2478\u001b[0;31m                               **self.session_kwargs)\n\u001b[0m\u001b[1;32m   2479\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mupdated\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2480\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m    893\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    894\u001b[0m       result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[0;32m--> 895\u001b[0;31m                          run_metadata_ptr)\n\u001b[0m\u001b[1;32m    896\u001b[0m       \u001b[0;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    897\u001b[0m         \u001b[0mproto_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1126\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1127\u001b[0m       results = self._do_run(handle, final_targets, final_fetches,\n\u001b[0;32m-> 1128\u001b[0;31m                              feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[1;32m   1129\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1130\u001b[0m       \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc\u001b[0m in \u001b[0;36m_do_run\u001b[0;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1342\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1343\u001b[0m       return self._do_call(_run_fn, self._session, feeds, fetches, targets,\n\u001b[0;32m-> 1344\u001b[0;31m                            options, run_metadata)\n\u001b[0m\u001b[1;32m   1345\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1346\u001b[0m       \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc\u001b[0m in \u001b[0;36m_do_call\u001b[0;34m(self, fn, *args)\u001b[0m\n\u001b[1;32m   1348\u001b[0m   \u001b[0;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1349\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1350\u001b[0;31m       \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1351\u001b[0m     \u001b[0;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1352\u001b[0m       \u001b[0mmessage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/bigdata/anaconda3/envs/py2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc\u001b[0m in \u001b[0;36m_run_fn\u001b[0;34m(session, feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[1;32m   1327\u001b[0m           return tf_session.TF_Run(session, options,\n\u001b[1;32m   1328\u001b[0m                                    \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1329\u001b[0;31m                                    status, run_metadata)\n\u001b[0m\u001b[1;32m   1330\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1331\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "model = get_textcnn(config['word_maxlen']*2,\n",
    "                        embed_weights, pretrain=False)\n",
    "\n",
    "for i in range(15):\n",
    "    if i == 8:\n",
    "        K.set_value(model.optimizer.lr, 0.0001)\n",
    "\n",
    "    model.fit_generator(\n",
    "        train_batch_generator(x_train, y_train, config['batch_size']),\n",
    "        epochs=1,\n",
    "        steps_per_epoch=int(x_train.shape[0] / config['batch_size']),\n",
    "        validation_data=(x_dev, y_dev),\n",
    "    \n",
    "    )\n",
    "\n",
    "    pred = model.predict(x_dev, batch_size=config['batch_size'])\n",
    "    pre, rec, f1 = score(y_dev, pred)\n",
    "\n",
    "    print('p r f1 ', pre, rec, f1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def get_textrnn(seq_length, embed_weight, pretrain=False):\n",
    "    main_input = Input(shape=(seq_length,), dtype='float64')\n",
    "    if pretrain:\n",
    "        embedding = Embedding(name='word_embedding', input_dim=config[\n",
    "            'vocab_size'], weights=[embed_weight], output_dim=config['w2v_vec_dim'], trainable=False)\n",
    "    else:\n",
    "        embedding = Embedding(name='word_embedding', input_dim=config[\n",
    "            'vocab_size'], output_dim=config['w2v_vec_dim'], trainable=True)\n",
    "    out = embedding(main_input)\n",
    "    out = Activation(activation=\"relu\")(\n",
    "        BatchNormalization()((TimeDistributed(Dense(256))(out))))\n",
    "    print('Build model...')\n",
    "    out = Bidirectional(GRU(256))(out)\n",
    "    #out= BatchNormalization(out)\n",
    "    out = Dropout(0.3)(out)\n",
    "    fc = Activation(activation=\"relu\")(\n",
    "        BatchNormalization()(Dense(256)(out)))\n",
    "    main_output = Dense(config['number_classes'],\n",
    "                        activation='softmax')(fc)\n",
    "\n",
    "    model = Model(inputs=main_input, outputs=main_output)\n",
    "    model.compile(optimizer='adam',\n",
    "                  loss='categorical_crossentropy',\n",
    "                  metrics=['accuracy'])\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Build model...\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 682s 2s/step - loss: 0.5495 - acc: 0.7704 - val_loss: 0.5360 - val_acc: 0.7629\n",
      "('p r f1 ', 0.34909090909090912, 0.11294117647058824, 0.17066666666666669)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 671s 2s/step - loss: 0.4976 - acc: 0.7841 - val_loss: 0.5197 - val_acc: 0.7776\n",
      "('p r f1 ', 0.44680851063829785, 0.12352941176470589, 0.19354838709677422)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 673s 2s/step - loss: 0.4629 - acc: 0.7953 - val_loss: 0.5173 - val_acc: 0.7751\n",
      "('p r f1 ', 0.44444444444444442, 0.16470588235294117, 0.24034334763948498)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 653s 2s/step - loss: 0.4329 - acc: 0.8070 - val_loss: 0.5357 - val_acc: 0.7677\n",
      "('p r f1 ', 0.41160220994475138, 0.17529411764705882, 0.24587458745874588)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 663s 2s/step - loss: 0.4004 - acc: 0.8234 - val_loss: 0.6243 - val_acc: 0.6574\n",
      "('p r f1 ', 0.28534482758620688, 0.38941176470588235, 0.32935323383084575)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 1123s 4s/step - loss: 0.3674 - acc: 0.8361 - val_loss: 0.6200 - val_acc: 0.6864\n",
      "('p r f1 ', 0.29704016913319237, 0.33058823529411763, 0.31291759465478841)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 1477s 5s/step - loss: 0.3410 - acc: 0.8485 - val_loss: 0.8609 - val_acc: 0.5372\n",
      "('p r f1 ', 0.25977238990598711, 0.61764705882352944, 0.36572622779519326)\n",
      "Epoch 1/1\n",
      "276/276 [==============================] - 1146s 4s/step - loss: 0.3017 - acc: 0.8668 - val_loss: 0.7011 - val_acc: 0.7266\n",
      "('p r f1 ', 0.33717579250720459, 0.2752941176470588, 0.30310880829015541)\n",
      "Epoch 1/1\n",
      " 78/276 [=======>......................] - ETA: 9:06 - loss: 0.3315 - acc: 0.8501"
     ]
    }
   ],
   "source": [
    "\n",
    "model = get_textrnn(config['word_maxlen']*2, embed_weights, pretrain=False)\n",
    "\n",
    "for i in range(15):\n",
    "    if i == 8:\n",
    "        K.set_value(model.optimizer.lr, 0.0001)\n",
    "\n",
    "    model.fit_generator(\n",
    "        train_batch_generator(x_train, y_train, config['batch_size']),\n",
    "        epochs=1,\n",
    "        steps_per_epoch=int(x_train.shape[0] / config['batch_size']),\n",
    "        validation_data=(x_dev, y_dev),\n",
    "    \n",
    "    )\n",
    "\n",
    "    pred = model.predict(x_dev, batch_size=config['batch_size'])\n",
    "    pre, rec, f1 = score(y_dev, pred)\n",
    "\n",
    "    print('p r f1 ', pre, rec, f1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:anaconda3]",
   "language": "python",
   "name": "conda-env-anaconda3-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
